package BasicKnowledge;

import java.util.Arrays;

public class arrayWork {


    public static void main1(String[] args) {
        int[] array1 = {1, 2, 3, 4};
        int[] array2 = new int[]{1, 2, 3, 4}; //前两者是直接进行了初始化
        int[] array3 = new int[10];  //只在堆上分配了引用对象所指向地址的空间，没有初始化


    }

    public static void main2(String[] args) {
        //两个数组指向一个对象，然后选择一个进行对对象的修改
        int[] array1 = {1, 2, 3, 4};
        array1[0] = 99;

        int[] array2 = array1; //此时array2 也指向 array1指向的引用地址；
        array2[0] = 100;  //array1指向的数组值被修改了
        System.out.println(Arrays.toString(array1));//{ 100，2，3，4}
        System.out.println(Arrays.toString(array2));//{ 100，2，3，4}

    }

    public static void main3(String[] args) {
        //数组回收
        int[] array1 = {1, 2, 3, 4};
        int[] array2 = {11, 22, 33, 44};
        array1 = array2;// array1 也指向了 array2指向的地址 ,此时array1原来指向的数组被回收
        array1[0] = 100;
        System.out.println(Arrays.toString(array1));//100  22 33 44
        System.out.println(Arrays.toString(array2));// 100 22 33 44

    }

    public static int[] grow(int[] array) {
        int[] tmpArray = new int[array.length];//新定义一个数组存放2倍后的数组
        for (int i = 0; i < array.length; i++) {
            tmpArray[i] = array[i] * 2; //将原数组的每个元素*2后，放到新数组中
        }
        return tmpArray; //返回新数组
    }

    public static void main4(String[] args) {
        //数组元素的二倍
        int[] array = {1, 2, 3, 4, 6, 7, 8};
        int[] ret = grow(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void func1(int[] array1) {
        array1 = new int[]{15, 16, 17};
    }

    public static void func2(int[] array2) {
        array2[0] = 99;
    }

    public static void main5(String[] args) {
        int[] array = {1, 2, 3};
        func1(array);
        System.out.println(Arrays.toString(array));
        // func2(array);
    }


    public static void main6(String[] args) {
        int[] array = {1, 2, 3, 4};
        int[] ret = Arrays.copyOf(array, array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));

        System.out.println("========");

        ret[0] = 999;

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));


    }
    public static void main7(String[] args) {
        int[] array = {1, 2, 3, 4};
        int[] ret = Arrays.copyOfRange(array, 1,3);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));

        System.out.println("========");

        ret[0] = 999;

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));


    }
    public static void main8(String[] args) {
        int[] array1 = {1, 2, 3, 4};
        int[] array2 = array1.clone();
        System.out.println(Arrays.toString( array1));
        System.out.println(Arrays.toString(array2));

        System.out.println("========");

        array2[0] = 999;

        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));


    }

}
